const express = require('express');
const shop_detail = express.Router();
const {ShopDetail,Menu,Comment} = require('../db/mongoose');

// 获取店铺详情
shop_detail.post('/getShopDetail',(req,res)=>{
    let shopId = req.body.id;
    console.log('shopid',shopId);
    ShopDetail.findOne({
        shop_id:shopId
    },(err,data)=>{
        if(err) return res.status(200).json({
            code:'3001',
            message:'系统错误'
        })
        if(JSON.stringify(data) == '{}') return res.status(200).json({
            code:'3002',
            message:'未找到数据'
        })

        return res.status(200).json({
            code:'3000',
            message:'成功找到数据',
            data
        })
    })
})

// 获取店铺菜单
shop_detail.post('/getMenu',(req,res)=>{
    let shopId = req.body.id;
    console.log(shopId);

    Menu.findOne({
        shop_id:shopId
    },(err,data)=>{
        if(err) return res.status(200).json({
            code:'4001',
            message:'系统错误'
        })
        if(JSON.stringify(data) == '{}') return res.status(200).json({
            code:'4002',
            message:'未找到数据'
        })
        return res.status(200).json({
            code:'4000',
            message:'成功找到数据',
            data:data.menuList
        })
    }).populate('menuList.detail')
})
shop_detail.post('/getFood',(req,res)=>{
    let foodId = req.body.id;
    console.log(foodId);

    Menu.findOne({
        'menuList.detail._id':foodId
    },(err,data)=>{
        if(err) return res.status(200).json({
            code:'4001',
            message:'系统错误'
        })
        if(JSON.stringify(data) == '{}') return res.status(200).json({
            code:'4002',
            message:'未找到数据'
        })
        return res.status(200).json({
            code:'4000',
            message:'成功找到数据',
            data:data.menuList
        })
    })
})

// 获取评论
shop_detail.post('/getComments',(req,res)=>{
    let shopId = req.body.id;
    console.log(shopId);

    Comment.find({
        shop_id:shopId
    },(err,data)=>{
        if(err) return res.status(200).json({
            code:'5001',
            message:'系统错误'
        })
        if(data.length == 0) return res.status(200).json({
            code:'5002',
            message:'未找到数据'
        })
        return res.status(200).json({
            code:'5000',
            message:'成功找到数据',
            data:data
        })
    })
})


module.exports = shop_detail;